﻿using MyToDo.Content.UnitOfWork.Collections;
using MyToDo.Shared.Parameters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace BZhanHengjicvWPF.Services
{
    public class BaseService<TEntity> : IBaseService<TEntity> where TEntity : class
    {
        private readonly HttpRestClient client;
        private readonly string serviceName;

        public BaseService(HttpRestClient client,string service)
        {
            this.client = client;
            this.serviceName = service;
        }
        public async Task<ApiResponse<TEntity>> AddAsync(TEntity entity)
        {
            BaseRequest baseRequest = new BaseRequest();
            baseRequest.Method = RestSharp.Method.POST;
            baseRequest.Route = $"api/{serviceName}/Add";
            baseRequest.Parameter = entity;
            return await client.ExcuteAsync<TEntity>(baseRequest);
        }

        public async Task<ApiResponse> DeleteAsync(int id)
        {
            BaseRequest baseRequest = new BaseRequest();
            baseRequest.Method = RestSharp.Method.DELETE;
            baseRequest.Route = $"api/{serviceName}/Delete?id={id}";
            return await client.ExcuteAsync(baseRequest);
        }

        public async Task<ApiResponse<PagedList<TEntity>>> GetAllAsync(QueryParameters queryable)
        {
            BaseRequest baseRequest = new BaseRequest();
            baseRequest.Method = RestSharp.Method.GET;
            baseRequest.Route = $"api/{serviceName}/GetAll?pageIndex={queryable.PageIndex}" +
                $"&pageSize={queryable.PageSize}&search={queryable.Search}";
            return await client.ExcuteAsync<PagedList<TEntity>>(baseRequest);
        }

        public async Task<ApiResponse<TEntity>> GetFirstOfDefaultAsync(int id)
        {
            BaseRequest baseRequest = new BaseRequest();
            baseRequest.Method= RestSharp.Method.GET;
            baseRequest.Route = $"api/{serviceName}/Get?id={id}";
            return await client.ExcuteAsync<TEntity>(baseRequest);
        }

        public async Task<ApiResponse<TEntity>> UpdateAsync(TEntity entity)
        {
            BaseRequest baseRequest = new BaseRequest();
            baseRequest.Method = RestSharp.Method.POST;
            baseRequest.Route = $"api/{serviceName}/Update";
            baseRequest.Parameter = entity;
            return await client.ExcuteAsync<TEntity>(baseRequest);
        }
    }
}
